package com.hanqi.service.impl;

import com.hanqi.dao.SysUserMapper;
import com.hanqi.entity.SysRole;
import com.hanqi.dao.SysRoleMapper;
import com.hanqi.entity.SysUser;
import com.hanqi.service.SysRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zjk
 * @since 2019-04-27
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    @Autowired
    protected SysUserMapper sysUserMapper;

    @Override
    public List<String> listRolePermissionsByUserId(String userId) {
        SysUser user = sysUserMapper.selectById(userId);
        if (user.getUserType().equals(1)) { // 超级管理员
            return baseMapper.listAllPermissions(); // 查出所有角色
        }
        return baseMapper.listRolePermissionsByUserId(userId);
    }

    @Override
    public List<Map<String, Object>> roleListByUserId(String userId) {
        List<Map<String, Object>> list = baseMapper.roleListByUserId(userId);
        for (Map<String, Object> map : list) {
            if (map.get("checked").equals("1")) {
                map.put("checked", true);
            } else {
                map.put("checked", false);
            }
        }
        return list;
    }
}
